package ciyuan.Item.SkillBook;

import ciyuan.Effect.Effects;
import ciyuan.Item.ItemGroups;
import ciyuan.Item.ItemRegisterManager;
import ciyuan.Item.TypeInterface.SkillBookType;
import ciyuan.Player.Skill.SkillUtils;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.AbstractArrowEntity;
import net.minecraft.entity.projectile.ArrowEntity;
import net.minecraft.item.ArrowItem;
import net.minecraft.item.CrossbowItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.potion.EffectInstance;
import net.minecraft.util.Hand;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;

/**
 * @author 150149
 */
public class ForcedOverload extends SkillBookType {

    public ForcedOverload() {
        super(new Properties().tab(ItemGroups.SkillBook));
    }

    @Override
    public void keyDown(PlayerEntity playerEntity) {
        SkillUtils.setSkillColdInterval(playerEntity, this, 35 * 1000);
        playerEntity.addEffect(new EffectInstance(Effects.force_overload, 15 * 20, 0));
        SkillUtils.THREAD_POOL_EXECUTOR.execute(new Runnable() {
            @Override
            public void run() {
                long time = System.currentTimeMillis();
                while (System.currentTimeMillis() - time < 15 * 1000) {
                    if (!playerEntity.getItemInHand(Hand.MAIN_HAND).isEmpty()) {
                        ItemStack itemStack = playerEntity.getItemInHand(Hand.MAIN_HAND);
                        if (itemStack.getItem() instanceof CrossbowItem) {
                            if (CrossbowItem.isCharged(itemStack)) {
                                addChargedProjectile(itemStack,new ItemStack(ItemRegisterManager.jian6.get()));
                            }
                        }
                    }
                    SkillUtils.sleep(100);
                }
                SkillUtils.saveSkillCastTime(playerEntity,"ForcedOverload");
            }
        });
    }

    @Override
    public String getSkillCastName() {
        return "ForcedOverload";
    }

    private static void addChargedProjectile(ItemStack p_220029_0_, ItemStack p_220029_1_) {
        CompoundNBT compoundnbt = p_220029_0_.getOrCreateTag();
        ListNBT listnbt = new ListNBT();
        CompoundNBT compoundnbt1 = new CompoundNBT();
        p_220029_1_.save(compoundnbt1);
        listnbt.add(compoundnbt1);
        compoundnbt.put("ChargedProjectiles", listnbt);
    }

    @Override
    public ITextComponent getSkillDescription() {
        return new TranslationTextComponent("ciyuan.skill.desc.31");
    }

    @Override
    public ITextComponent getSkillName() {
        return new TranslationTextComponent("ciyuan.skill.name.31");
    }

    public static class MyArrow3 extends ArrowItem {
        public MyArrow3() {
            super(new Properties());
        }

        @Override
        public AbstractArrowEntity createArrow(World p_200887_1_, ItemStack p_200887_2_, LivingEntity p_200887_3_) {
            ArrowEntity arrowentity = new MyArrow3.MyArrowentity(p_200887_1_, p_200887_3_);
            arrowentity.setPierceLevel((byte) 10);
            return arrowentity;
        }

        public static class MyArrowentity extends ArrowEntity {

            public MyArrowentity(World p_i46758_1_, LivingEntity p_i46758_2_) {
                super(p_i46758_1_, p_i46758_2_);
            }

            @Override
            protected ItemStack getPickupItem() {
                return new ItemStack(Items.ARROW);
            }

            @Override
            protected void doPostHurtEffects(LivingEntity p_184548_1_) {
                super.doPostHurtEffects(p_184548_1_);
                p_184548_1_.addEffect(new EffectInstance(net.minecraft.potion.Effects.MOVEMENT_SLOWDOWN,300,2));
            }

            @Override
            public int getColor() {
                return 16284963;
            }
        }

    }
}
